const express = require("express")
const router = express.Router()
const time = require("../../utils/time")
const db = require("../../sqlServer/dbConnect")

// 供应商信息-- 查询
router.post("/info", (req, res) => {
    const { name, pageConfig ,doName} = req.body;
    const { page, pageSize } = pageConfig
    let sql = name === undefined ? "select * from supplier order by Id limit ?,?;select * from supplier"
        : "select * from supplier where suName=?"
    const data = name === undefined ? [(Number(page) - 1) * Number(pageSize), Number(pageSize)] : [name]
    db.sqlQuery(sql, data, (err, result) => {
        if (err) {
            console.log(err)
            res.send({
                // data: result,
                msg: "获取信息失败",
                status: 203
            })
        } else {
            name === undefined ?
                res.send({
                    data: result[0],
                    total: result[1].length,
                    msg: "获取信息成功",
                    status: 200
                }) :
                res.send({
                    data: result,
                    total: result.length,
                    msg: "获取信息成功",
                    status: 200
                })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"供应商信息查询",time.getData()])
        }
    })
})

// 供应商信息-- 添加
router.post("/add", (req, res) => {
    const {suName,suLeader,suPhone,suPlace,suConStart,suConEnd,doName } = req.body
    let sql = `insert into supplier(suName,suLeader,suPhone,suPlace,suConStart,suConEnd) values(?,?,?,?,?,?)`
    const data = [suName,suLeader,suPhone,suPlace,suConStart,suConEnd]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
        } else {
            res.send({
                msg: "添加成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"供应商信息添加",time.getData()])
        }
    })
})

// 供应商信息-- 编辑
router.post("/edit", (req, res) => {
    let {suName,suLeader,suPhone,suPlace,suConStart,suConEnd,Id,doName } = req.body
    let sql = `update supplier set suName=?,suLeader=?,suPhone=?,suPlace=?,suConStart=?,suConEnd=? where Id=?`
    const data = [suName,suLeader,suPhone,suPlace,suConStart,suConEnd,Id ]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "编辑失败",
                status: 203
            })
        } else {
            res.send({
                msg: "编辑成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"供应商信息编辑",time.getData()])
        }
    })
})

// 供应商信息-- 删除
router.post("/delete", (req, res) => {
    const { Id,doName } = req.body
    let sql = "delete from supplier where Id=?"
    const data = [Id]
    db.sqlQuery(sql, data, (err) => {
        if (err) {
            console.log(err)
            res.send({
                msg: "删除失败",
                status: 203
            })
        } else {
            res.send({
                msg: "删除成功",
                status: 200
            })
            db.sqlQuery("insert into log(doName,doInfo,doTime) values(?,?,?)",[doName,"供应商信息删除",time.getData()])
        }
    })
})

module.exports = router